package com.oig.sys.auth.service.impl;

import com.oig.common.constants.CommonEnum;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.auth.dao.ITbRoleSystemDao;
import com.oig.sys.auth.dao.ITbSystemDao;
import com.oig.sys.auth.dto.SystemDto;
import com.oig.sys.auth.entity.TbRoleSystem;
import com.oig.sys.auth.service.ITbRoleSystemService;
import com.oig.sys.auth.vo.SystemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuxl
 */
@Service
public class TbRoleSystemServiceImpl implements ITbRoleSystemService {

	private final ITbRoleSystemDao tbRoleSystemDao;
	private final ITbSystemDao systemDao ;

	@Autowired
	public TbRoleSystemServiceImpl(ITbRoleSystemDao tbRoleSystemDao,ITbSystemDao systemDao){
		 this.tbRoleSystemDao = tbRoleSystemDao;
		 this.systemDao = systemDao;
	}

	@Override
	public List<SystemVo> findSystemByRole(Integer roleId) {
		List<Integer> systemIdList = this.tbRoleSystemDao.findSystemIdByRole(roleId);
		return systemDao.findAllByIds(systemIdList);
	}

	@Override
	public List<SystemVo> findOkSystemByRole(List<Integer> roleIdList) {
		List<Integer> systemIdList = this.findOkSystemIdByRole(roleIdList);
		return systemDao.findAllByIds(systemIdList);
	}

	@Override
	public List<Integer> findOkSystemIdByRole(List<Integer> roleIdList) {
		return this.tbRoleSystemDao.findSystemIdByRole(roleIdList, CommonEnum.CommonStatusEnum.OK.getCode());
	}

	/**
	 * 数据量很小先这么写
	 */
	@Transactional
	@Override
	public List<SystemVo> addRoleSystem(Integer roleId, List<SystemDto> systemDtoList) {
		List<Integer> systemIdList = new ArrayList<>();
		for (SystemDto systemDto : systemDtoList){
			if (systemDto.getId()==null){
				throw new BadRequestException("系统id为空");
			}
			TbRoleSystem roleSystem = new TbRoleSystem();
			roleSystem.setRoleId(roleId);
			roleSystem.setSysId(systemDto.getId());
			this.tbRoleSystemDao.save(roleSystem) ;
			systemIdList.add(roleSystem.getSysId()) ;
		}
		return systemDao.findAllByIds(systemIdList);
	}

	@Transactional
	@Override
	public void addRoleSystemId(Integer roleId, List<Integer> systemIdList) {
		for (Integer systemId : systemIdList) {
			TbRoleSystem roleSystem = new TbRoleSystem();
			roleSystem.setRoleId(roleId);
			roleSystem.setSysId(systemId);
			this.tbRoleSystemDao.save(roleSystem);
		}
	}

	@Transactional
	@Override
	public void delRoleSystemId(Integer roleId, List<Integer> systemIdList) {
		List<Integer> delIdList = this.tbRoleSystemDao.findAllDelId(roleId, systemIdList);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbRoleSystemDao.deleteByIdList(delIdList) ;
		}
	}

	@Transactional
	@Override
	public List<SystemVo> updateRoleSystem(Integer roleId, List<SystemDto> systemDtoList) {
		List<Integer> systemIdList = new ArrayList<>();
		List<Integer> roleSystemIdList = new ArrayList<>() ;
		for (SystemDto systemDto : systemDtoList) {
			if (systemDto.getId()==null){
				throw new BadRequestException("系统id为空");
			}
			TbRoleSystem roleSystem = this.tbRoleSystemDao.getByRoleIdAndSysId(roleId, systemDto.getId()) ;
			if (roleSystem==null){
				roleSystem = new TbRoleSystem();
				roleSystem.setRoleId(roleId);
				roleSystem.setSysId(systemDto.getId());
				this.tbRoleSystemDao.save(roleSystem) ;
			}
			roleSystemIdList.add(roleSystem.getId()) ;
			systemIdList.add(roleSystem.getSysId()) ;
		}
		//如果有索引不用这么麻烦
		List<Integer> delIdList = this.tbRoleSystemDao.findAllDelId(roleId, roleSystemIdList);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbRoleSystemDao.deleteByIdList(delIdList) ;
		}
		return systemDao.findAllByIds(systemIdList);
	}

	@Transactional
	@Override
	public void delRoleSystemByRoleId(Integer roleId) {
		List<Integer> delIdList = this.tbRoleSystemDao.findAllIdByRoleId(roleId) ;
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbRoleSystemDao.deleteByIdList(delIdList);
		}
	}

	@Transactional
	@Override
	public void delRoleSystemBySystemId(Integer systemId) {
		List<Integer> delIdList = this.tbRoleSystemDao.findAllIdBySystemId(systemId) ;
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbRoleSystemDao.deleteByIdList(delIdList);
		}
	}
}
